package com.example.sharesinfo.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.androidkun.xtablayout.XTabLayout;
import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.MyPagerAdapter;
import com.example.sharesinfo.base.BaseFragment;
import com.example.sharesinfo.fragment.stock.MarketAStocksFragment;
import com.example.sharesinfo.fragment.stock.MarketHongKongStocksFragment;
import com.example.sharesinfo.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class StockFragment extends BaseFragment {

    private StockFragment fa;

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private XTabLayout mTableLayout;
    private ViewPager mViewPager;
    private MarketAStocksFragment mMarketAStocksFragment;
    private MarketHongKongStocksFragment mMarketHongKongStocksFragment;
    private String tag;


    public StockFragment getFragment() {
        if (fa == null) {
            fa = new StockFragment();
        }
        return fa;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stock, null);
        initData();
        initView(view);
        return view;
    }

    private void initData() {
        mMarketAStocksFragment = new MarketAStocksFragment();
        mMarketHongKongStocksFragment = new MarketHongKongStocksFragment();
        fragments.add(mMarketAStocksFragment);
        fragments.add(mMarketHongKongStocksFragment);
        titles.add("A股");
        titles.add("港股");

    }

    private void initView(View view) {

        mTableLayout = view.findViewById(R.id.market_tl_tabs);
        mViewPager = view.findViewById(R.id.market_vp_view);
        mViewPager.setAdapter(new MyPagerAdapter(getFragmentManager(), getContext(), fragments, titles));
        mTableLayout.setupWithViewPager(mViewPager);//此方法就是让tablayout和ViewPager联动

    }

    @Override
    public void onResume() {
        super.onResume();
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        RadioButton main_market = activity.findViewById(R.id.radio_button_market);
        tag = main_market.getTag().toString();
        LogUtils.i("股票选择是沪还是港");
        if (tag.equals("global")) {
            mTableLayout.getTabAt(1).select();
        } else {
            mTableLayout.getTabAt(0).select();
        }
    }
}
